<template>
  <div class="app-container">
    <h4 class="form-header h4">基本信息</h4>
    <el-form ref="form" :model="form" label-width="80px">
      <el-row>
        <el-col :span="8" :offset="2">
          <el-form-item label="房屋编号" prop="houseId">
            <el-input v-model="form.houseId" disabled/>
          </el-form-item>
        </el-col>
        <el-col :span="8" :offset="2">
          <el-form-item label="房屋名称" prop="houseName">
            <el-input v-model="form.houseName" disabled/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <h4 class="form-header h4">分配操作(标红的行为当前负责人)</h4>
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="130px">
      <el-form-item label="中介人(用户)编号" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入中介人(用户)编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="mediumList.slice((pageNum-1)*pageSize,pageNum*pageSize)"
              ref="table" :row-style="isAllocated">
      <el-table-column label="序号" type="index" align="center">
        <template slot-scope="scope">
          <span>{{ (pageNum - 1) * pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="房屋中介编号" align="center" prop="userId"/>
      <el-table-column label="房屋中介名" align="center" prop="username"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-star-on"
            @click="handleAllocat(scope.row)"
            v-hasPermi="['domain:medium:edit']"
            v-if="!scope.row.flag"
          >授权
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-form label-width="100px">
      <el-form-item style="text-align: center;margin-left:-120px;margin-top:30px;">
        <el-button @click="close()">返回</el-button>
      </el-form-item>
    </el-form>

  </div>
</template>

<script>
import {listMedium, listMediumAllocat, updateMedium} from "@/api/system/medium";
import modal from "@/plugins/modal";

export default {
  name: "Medium",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 房屋中介表表格数据
      mediumList: [],
      //房屋中介表表格数据搜索后数据
      mediums: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 分页信息
      pageNum: 1,
      pageSize: 10,
      //负责当前房源的房屋中介编号
      allocatedUserId: "",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    // this.getList();
    const houseId = this.$route.params && this.$route.params.houseId;
    if (houseId) {
      this.loading = true;
      listMediumAllocat(houseId).then((response) => {
        //查出所有房屋中介
        this.mediumList = response.mediumList;
        this.form = response.house;
        this.total = this.mediumList.length;
        this.$nextTick(() => {
          this.mediumList.forEach((row) => {
            if (row.flag) {
              this.allocatedUserId = row.userId;
            }
          });
        });
        this.loading = false;
      });
    }
  },
  methods: {
    /** 查询房屋中介表列表 */
    getList() {
      this.loading = true;
      listMedium(this.queryParams).then(response => {
        this.mediumList = response.rows;
        this.total = response.total;
        this.$nextTick(() => {
          this.mediumList.forEach((row) => {
            if (row.userId === this.allocatedUserId) {
              row.flag = true;
            }
          });
        });
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 关闭按钮 */
    close() {
      const obj = {path: "/house/houses"};
      this.$tab.closeOpenPage(obj);
    },
    handleAllocat(row) {
      this.allocatedUserId = row.userId;
      updateMedium(row.userId, this.form.houseId).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.close();
      });
    },
    //判断当前行房屋中介是否负责该房源
    isAllocated(row) {
      if (row.row.flag) {
        return {
          color: "red",
        };
      }
    }
  }
};
</script>
